package com.wizardOther.Items;

import com.table.CarTableController2;
import Validator.Validator;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.Model.CarsModel;
import com.wizardOther.OtherControlledScreen;
import com.wizardOther.OtherScreensController;
import com.wizardOther.OtherScreensFramework;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 *
 * @author Vi
 */
public class AddNewCarController extends ValidationForm implements Initializable, OtherControlledScreen {

    private OtherScreensController myController;
    @FXML
    private Button addCar;
    @FXML
    private Button cancel;
    @FXML
    private Button select;
    @FXML
    private TextField plate;
    @FXML
    private TextField make;
    @FXML
    private TextField model;
    @FXML
    private ComboBox carType;
    @FXML
    private TextArea comment;
    @FXML
    private Label labelPlate;
    @FXML
    private Label labelMake;
    @FXML
    private Label labelModel;
    @FXML
    private Label labelCarType;
    @FXML
    private Label labelComment;
    private Integer selectedCar;
    private CarTableController2 controller;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        inputs.clear();
        inputs.add(plate);
        inputs.add(make);
        inputs.add(model);
        inputs.add(carType);
        inputs.add(comment);

        labels.clear();
        labels.add(labelPlate);
        labels.add(labelMake);
        labels.add(labelModel);
        labels.add(labelCarType);
        labels.add(labelComment);

    }

    private void clearForm() {
        this.selectedCar = null;
        this.plate.setText("");
        this.make.setText("");
        this.model.setText("");
        this.carType.setValue("Wybierz typ pojazdu");
        this.comment.setText("");
        makeList(5);
        setWrongTextFields(correctField);
    }

    @FXML
    private void handleButtonAction(ActionEvent event) {
        if (event.getSource() == addCar) {
            validateForm();
        }
        if (event.getSource() == cancel) {
            clearForm();
        }
        if (event.getSource() == select) {
            showCarTableDialog();

            selectedCar = controller.getSelectedCar();

            if (this.selectedCar != null) {
                loadData(selectedCar);
            }

        }
    }

    private void loadData(Integer selectedCar) {
        CarsModel car = DaoInstanceEnum.INSTANCE.getDao().getCarById(selectedCar);
        System.out.println(car.toString());
        if (car != null) {
            this.plate.setText(car.getNumberPlate());
            this.make.setText(car.getMake());
            this.model.setText(car.getModel());
            this.carType.setValue(car.getCarType().toString());
            this.comment.setText(car.getComments());

        }
    }

    private void validateForm() {
        makeList(5);
        takeDateFromForm();
        setWrongTextFields(correctField);

        printValidator(correctField);
        if (isCorrect(correctField)) {

            List<Boolean> goodRecord = Validator.checkCarCheckList(getCar());
            setWrongTextFields(goodRecord);

            if (isCorrect(goodRecord)) {

                try {
                    DAO dao = DaoInstanceEnum.INSTANCE.getDao();
                    if (selectedCar != null) {
                        CarsModel car = getCar();
                        car.setCarId(selectedCar);
                        dao.updateCar(car);
                        createPopup("Zapisano zmiany!");
                    } else {
                        dao.addCar(getCar());
                        createPopup("Dodano nowy pojazd!");
                        this.selectedCar = null;
                    }

                    clearForm();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private void takeDateFromForm() {

        checkString(this.plate.getText(), 0);
        checkString(this.make.getText(), 1);
        checkString(this.model.getText(), 2);
        checkString(renameCarType(this.carType.getValue().toString()), 3);
        checkComments(this.comment.getText(), 4);

    }

    @FXML
    private void goToMain(java.awt.event.ActionEvent event) {
        myController.setScreen(OtherScreensFramework.OTHER_BUTTONS_PANEL);
    }

    private CarsModel getCar() {
        String numberPlate = this.plate.getText();
        String makeText = this.make.getText();
        String modelText = this.model.getText();
        String carTypeText = renameCarType(this.carType.getValue().toString());
        String commentsText = this.comment.getText();
        CarsModel car = new CarsModel(numberPlate, makeText, modelText, carTypeText);
        car.setComments(commentsText);
        System.out.println("Plate:"+car.getNumberPlate()+", carType"+car.getCarType()+", make"+car.getMake()+", model "+car.getModel()+"comment: "+car.getComments());

        return car;
    }

    @Override
    public void setScreenParent(OtherScreensController screenParent) {
        myController = screenParent;
    }

    private boolean showCarTableDialog() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("carTable2.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Wybierz pojazd");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            controller = loader.getController();
            controller.setDialogStage(dialogStage);

            dialogStage.showAndWait();

            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
